package com.example.demo.utils;

import com.example.demo.bean.SystemConfig;
import com.ideabank.po.SOFInterface;
import com.ideabank.po.TSAInterface;
import com.ideabank.po.mof.MOFClient;
import com.ideabank.po.mof.exception.NewCSSException;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

@Slf4j
public class InterfaceUtil {
    private static SOFInterface svc=null;

    private static TSAInterface tsa=null;

    private static MOFClient mof=null;

    public static SOFInterface getAppInstance(){
        if(svc==null) {
            String uri= "http://" + SystemConfig.getIp() + ":" + SystemConfig.getPort() + "/" ;

            log.error("getAppInstance uri:{}; config KeyIndex:{}, KeyValue:{}, ContainerName:{}, PolicyName:{} ", uri, SystemConfig.getKeyIndex(), SystemConfig.getKeyValue(), SystemConfig.getContainerName(),SystemConfig.getPolicyName());

            svc =SOFInterface.SOF_getAppInstance(uri,SystemConfig.getKeyIndex(), SystemConfig.getKeyValue(),SystemConfig.getContainerName(),SystemConfig.getPolicyName());
            return svc!=null?svc:null;
        }
        else{
            return svc;
        }
    }
    public static TSAInterface getTSAInterface(){
        if(tsa==null) {
            tsa =new TSAInterface(SystemConfig.getIp(),SystemConfig.getPort());
            return tsa!=null?tsa:null;
        }
        else{
            return tsa;
        }
    }

    public static MOFClient getMOFInterface(){
        if(mof==null) {
            try {
                mof =new MOFClient(SystemConfig.getIp()+":"+SystemConfig.getPort());
            } catch (NewCSSException e) {
                e.printStackTrace();
                System.out.println("错误码："+e.getCode()+"错误信息："+e.getDescription());
                return null;
            }
            return mof!=null?mof:null;
        }
        else{
            return mof;
        }
    }
}
